import { symlinkCommon } from "./Utils";
import { WebSocketServer } from "ws";
import { ApiMsgEnum, IApiPlayerJoinReq, IApiPlayerJoinRes, IApiPlayerListReq, IApiPlayerListRes, IApiRoomCreateReq, IApiRoomCreateRes, IApiRoomListReq, IApiRoomListRes } from "./Common";
import { Connection, MyServer } from "./Core";
import { PlayerManager } from "./Biz/PlayerManager";
import { RoomManager } from "./Biz/RoomManager";
import { Player } from "./Biz/Player";



symlinkCommon();
declare module "./Core" {
    interface Connection {
        playerId: number
    }
}

const server = new MyServer({
    port: 9876,

})

server.on("connection", () => {
    console.log("index lairen le ", server.connections.size)
})

server.on("disconnection", (connection: Connection) => {
    console.log("index zouren le ", server.connections.size)

    if (connection.playerId) {

        PlayerManager.Instance.removePlayer(connection.playerId)
    }
    PlayerManager.Instance.syncPlayers()

})

server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, data: IApiPlayerJoinReq): IApiPlayerJoinRes => {

    const { nickname } = data
    const player = PlayerManager.Instance.createPlayer({ nickname, connection })
    connection.playerId = player.id
    PlayerManager.Instance.syncPlayers()
    return {
        player: PlayerManager.Instance.getPlayerView(player),
    }
})


server.setApi(ApiMsgEnum.ApiPlayerList, (connection: Connection, data: IApiPlayerListReq): IApiPlayerListRes => {
    return {
        list: PlayerManager.Instance.getPlayersView()
    }
})

server.setApi(ApiMsgEnum.ApiRoomList, (connection: Connection, data: IApiRoomListReq): IApiRoomListRes => {
    return {
        list: RoomManager.Instance.getRoomsView()
    }
})

server.setApi(ApiMsgEnum.ApiRoomCreate, (connection: Connection, data: IApiRoomCreateReq): IApiRoomCreateRes => {
    if (connection.playerId) {
        const newRoom = RoomManager.Instance.createRoom()
        console.log("newRoom.id", newRoom.id)
        const room = RoomManager.Instance.joinRoom(newRoom.id, connection.playerId)

        if (room) {
            PlayerManager.Instance.syncPlayers()
            RoomManager.Instance.syncRooms()
            return {
                room: RoomManager.Instance.getRoomView(room)
            }
        }
        else {
            throw new Error("房间不存在")
        }
    }
    else {
        throw new Error("未登录")
    }


    // return {
    //     list: PlayerManager.Instance.getPlayersView()
    // }
})

server
    .start()
    .then(() => {
        console.log('服务启动！')
    })
    .catch((e) => {
        console.log(e)
    })


// const wss = new WebSocketServer({
//     port: 9876
// })

// let inputs = []
// wss.on("connection", (socket) => {
//     socket.on("message", (buffer) => {
//         const str = buffer.toString()
//         try {
//             const msg = JSON.parse(str)
//             const { name, data } = msg
//             const { frameId, input } = data
//             inputs.push(input)
//         } catch (error) {
//             console.log(error)
//         }

//     })

//     // setInterval(() => {
//     //     const temp = inputs;
//     //     inputs = []
//     //     const msg = {
//     //         name: ApiMsgEnum.MsgServerSync,
//     //         data: {
//     //             inputs: temp,
//     //         }
//     //     }
//     //     socket.send(JSON.stringify(msg))

//     // }, 100)

// })

// wss.on("listening", () => {
//     console.log("服务启动!!!")
// })